package com.atom.app.lifecycle.base

import android.content.Context
import android.content.res.Configuration
import com.atom.app.lifecycle.SplashActivity
import com.atom.app.lifecycle.utils.LanguageUtil
import com.atom.module.logger.Logger
import com.atom.module.mvvm.base.AbstractApplication
import com.atom.module.reporter.CrashReporter
import com.blankj.utilcode.util.LanguageUtils
import com.blankj.utilcode.util.Utils
import com.tencent.mmkv.MMKV

class AppApplication : AbstractApplication() {

    companion object {
        private lateinit var instance: AppApplication

        fun getInstance(): AppApplication {
            return instance
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        // Logger init
        Logger.init(this, isDebug = true)
        // CrashReport
        CrashReporter.initialize(this)
        CrashReporter.setRestartActivity(SplashActivity::class.java)
        CrashReporter.setReportHandler { error, callback ->
            Logger.e(error)
            callback.onCallback("Success")
        }
        // init MMKV
        MMKV.initialize(this)
        // Utils
        Utils.init(this)
    }


    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(LanguageUtil.attachBaseContext(base))
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        // 系统资源配置发生更改，例如主题模式，需要重新刷新多语言
        LanguageUtil.attachBaseContext(this);
    }


}